﻿using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors;
using System.Windows.Media;

namespace Base.ModuleBase.Behaviors
{
    public class PasswordBehavior:Behavior<PasswordBox>
    {

        public string EmptyText
        {
            get { return (string)GetValue(EmptyTextProperty); }
            set { SetValue(EmptyTextProperty, value); }
        }

        public static readonly DependencyProperty EmptyTextProperty =
            DependencyProperty.Register("EmptyText", typeof(string), typeof(PasswordBehavior), new PropertyMetadata(null, OnEmptyTextChanged));



        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(PasswordBehavior), new PropertyMetadata(null,OnPasswdSourceChanged));

        private static void OnPasswdSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var bhv = d as PasswordBehavior;
            if (bhv == null) return;
            if (bhv.Text != bhv.AssociatedObject.Password)
                bhv.AssociatedObject.Password = bhv.Text;
        }


        protected override void OnAttached()
        {
            base.OnAttached();
            SetEmptyTextHandler();
            CheckText();
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.RemoveHandler(PasswordBox.PasswordChangedEvent,_handler);
        }

        private void AssociatedObject_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (AssociatedObject.Password != Text)
                Text = AssociatedObject.Password;
            if (EmptyText == null) 
                return;
            CheckText();
        }

        private static void OnEmptyTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var bhv = d as PasswordBehavior;
            if (bhv != null)
            {
                bhv.SetEmptyTextBrush();
                if (bhv.AssociatedObject != null)
                    bhv.CheckText();
            }
        }


        private Brush _originBrush;
        private Brush _emptyBrush;
        private RoutedEventHandler _handler;

        private void SetEmptyTextHandler()
        {
            if (_handler == null)
            {
                _handler = AssociatedObject_PasswordChanged;
                AssociatedObject.AddHandler(PasswordBox.PasswordChangedEvent, _handler, true);
            }
        }

        private void SetEmptyTextBrush()
        {
            _emptyBrush = TextBrush(EmptyText);
        }

        private void CheckText()
        {
            if (AssociatedObject.Password.Length < 1 && !Equals(AssociatedObject.Background, _emptyBrush))
            {
                _originBrush = AssociatedObject.Background;
                AssociatedObject.Background = _emptyBrush;
            }
            else if (!Equals(AssociatedObject.Background, _originBrush))
            {
                AssociatedObject.Background = _originBrush;
            }
        }

        static Brush TextBrush(string text)
        {
            TextBlock tb = new TextBlock();
            tb.Foreground = Brushes.Black;
            tb.FontSize = 16;
            tb.Text = text;
            VisualBrush vb = new VisualBrush(tb);
            vb.Stretch = Stretch.None;
            vb.Opacity = 0.7;
            vb.AlignmentX = AlignmentX.Left;
            vb.AlignmentY = AlignmentY.Center;
            return vb;
        }
    }
}
